package com.aptemo.webetek.client.widgets.games;

import com.aptemo.webetek.client.enums.EnumEventStatus;
import com.aptemo.webetek.client.events.DecisionEvent;
import com.aptemo.webetek.client.events.DecisionHandler;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.aptemo.webetek.client.events.PickClickEvent;
import com.aptemo.webetek.shared.EventBean;
import com.aptemo.webetek.shared.PickBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

public class GameResultsDialog
		extends DialogBox implements HasDecisionHandlers
{

	private static GameResultsUiBinder	uiBinder	= GWT.create(GameResultsUiBinder.class);

	interface GameResultsUiBinder extends UiBinder<Widget, GameResultsDialog>
	{
	}

	@UiField
	Label				infoField;

	@UiField
	BetPicker			betPicker;

	@UiField
	TextArea			resultsComment;

	@UiField
	Label				pickedResultField;

	@UiField
	Button				submitButton;

	@UiField
	Button				cancelButton;

	private String		info;

	private PickBean	pick;

	public GameResultsDialog()
	{
		setText("Select a winner...");
		add(uiBinder.createAndBindUi(this));

		info = infoField.getText();
	}

	public void setData(EventBean bet)
	{
		betPicker.setData(bet);

		if (bet.getStatus() == EnumEventStatus.CLOSED)
		{
			resultsComment.setText(bet.getResultDescription());

			pick = bet.getWinPick();
			// betPicker.setWinner();
			if (pick != null)
			{
				pickedResultField.setText(pick.getTitle());
			}
		}
	}

	@UiHandler("resultsComment")
	void textBoxclickHandler(ClickEvent event)
	{
		event.stopPropagation();
	}

	@UiHandler("betPicker")
	void betPickerHandler(PickClickEvent event)
	{
		pick = event.getPick();

		pickedResultField.setText(pick.getTitle());
	}

	@UiHandler("cancelButton")
	void cancelClickHandler(ClickEvent event)
	{
		pickedResultField.setText(info);
		hide();
	}

	@UiHandler("submitButton")
	void submitClickHandler(ClickEvent event)
	{
		// check field...
		if (pick == null)
		{
			infoField.setText(info);
			return;
		}
		if (resultsComment.getText().isEmpty())
		{
			infoField.setText("Please, enter a comment...");
			return;
		}
		fireEvent(new DecisionEvent(infoField));
	}

	public HasText getCommentField()
	{
		return resultsComment;
	}

	public PickBean getSelectedPick()
	{
		return pick;
	}

	@Override
	public HandlerRegistration addDecisionHandler(DecisionHandler handler)
	{
		return addHandler(handler, DecisionEvent.TYPE);
	}

}
